package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbAdvertisement;
import cn.stylefeng.guns.modular.cb.mapper.CbAdvertisementMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbAdvertisementParam;
import cn.stylefeng.guns.modular.cb.model.result.CbAdvertisementResult;
import  cn.stylefeng.guns.modular.cb.service.CbAdvertisementService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ss
 * @since 2021-01-30
 */
@Service
public class CbAdvertisementServiceImpl extends ServiceImpl<CbAdvertisementMapper, CbAdvertisement> implements CbAdvertisementService {

    @Override
    public void add(CbAdvertisementParam param){
        CbAdvertisement entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CbAdvertisementParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbAdvertisementParam param){
        CbAdvertisement oldEntity = getOldEntity(param);
        CbAdvertisement newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbAdvertisementResult findBySpec(CbAdvertisementParam param){
        return null;
    }

    @Override
    public List<CbAdvertisementResult> findListBySpec(CbAdvertisementParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbAdvertisementParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<CbAdvertisementResult> advertisementList() {
        return this.baseMapper.advertisementList();
    }

    private Serializable getKey(CbAdvertisementParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbAdvertisement getOldEntity(CbAdvertisementParam param) {
        return this.getById(getKey(param));
    }

    private CbAdvertisement getEntity(CbAdvertisementParam param) {
        CbAdvertisement entity = new CbAdvertisement();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
